/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa036is.daoimplementation;

import cz.muni.fi.pa036is.daointerface.AddressDAO;
import cz.muni.fi.pa036is.entity.Address;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Kuba
 */
@Repository
public class AddressDAOImpl implements AddressDAO {

    private Connection con = null;
    private Statement st = null;
    private ResultSet rs = null;

    public AddressDAOImpl() throws ClassNotFoundException {

        String url = "jdbc:postgresql://localhost/PA036";
        String user = "postgres";
        String password = "test";

        try {
												Class.forName("org.postgresql.Driver");
            con = DriverManager.getConnection(url, user, password);
        } catch (SQLException ex) {
            Logger.getLogger(AddressDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void addAddress(Address address) {
        if (address == null) {
            throw new IllegalArgumentException("Address can not be null");
        }
        
        try {
            String insert = "INSERT INTO address "
                    + "(person_id, street, city, zip_code, country) VALUES"
                    + "(?,?,?,?,?)";
            PreparedStatement preparedStatement = con.prepareStatement(insert);

            preparedStatement.setLong(1, address.getPersonId());
            preparedStatement.setString(2, address.getStreet());
            preparedStatement.setString(3, address.getCity());
            preparedStatement.setString(4, address.getZipCode());
            preparedStatement.setString(5, address.getCountry());
            preparedStatement.execute();
												

        } catch (SQLException ex) {
            Logger.getLogger(AddressDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void editAddress(Address address) {
        if (address == null) {
            throw new IllegalArgumentException("Address can not be null");
        }
        try {

            String update = "UPDATE address SET person_id= ?, street= ?, city= ?, zip_code= ?, country= ? WHERE id= ?";

            PreparedStatement preparedStatement = con.prepareStatement(update);

            preparedStatement.setLong(1, address.getPersonId());
            preparedStatement.setString(2, address.getStreet());
            preparedStatement.setString(3, address.getCity());
            preparedStatement.setString(4, address.getZipCode());
            preparedStatement.setString(5, address.getCountry());
            preparedStatement.setLong(6, address.getId());

            preparedStatement.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(AddressDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void deleteAddress(Long id) {
        if (id == null) {
            throw new IllegalArgumentException("ID can not be null");
        }
        try {

            String delete = "DELETE FROM address WHERE id=?";

            PreparedStatement preparedStatement = con.prepareStatement(delete);

            preparedStatement.setLong(1, id);

            preparedStatement.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(AddressDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public Address getAddressByPerson(Long id) {
        if (id == null) {
            throw new IllegalArgumentException("ID can not be null");
        }
        Address address = new Address();
        try {

            String find = "SELECT * FROM address WHERE person_id=?";

            PreparedStatement preparedStatement = con.prepareStatement(find);

            preparedStatement.setLong(1, id);

            rs = preparedStatement.executeQuery();

            rs.next();
            address.setId(rs.getLong(1));
            address.setPersonId(rs.getLong(2));
            address.setStreet(rs.getString(3));
            address.setCity(rs.getString(4));
            address.setZipCode(rs.getString(5));
            address.setCountry(rs.getString(6));

        } catch (SQLException ex) {
            Logger.getLogger(AddressDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return address;
    }

    @Override
    public List<Address> getAllAddresses() {
        List<Address> addressList = new ArrayList<Address>();

        try {

            String getAll = "SELECT * FROM address";

            PreparedStatement preparedStatement = con.prepareStatement(getAll);

            rs = preparedStatement.executeQuery();

            while (rs.next()) {
                Address address = new Address();
                address.setId(rs.getLong(1));
                address.setStreet(rs.getString(2));
                address.setCity(rs.getString(3));
                address.setZipCode(rs.getString(4));
                address.setCountry(rs.getString(5));
                addressList.add(address);

            }
        } catch (SQLException ex) {
            Logger.getLogger(AddressDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return addressList;
    }
}
